<template>
  <div class="my-claims-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>我的认领</span>
        </div>
      </template>

      <el-table :data="tableData" v-loading="loading">
        <el-table-column prop="requestNo" label="申请编号" width="180" />
        <el-table-column prop="applicantName" label="申请人姓名" width="120" />
        <el-table-column label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusName(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="claimReason" label="认领理由" show-overflow-tooltip />
        <el-table-column label="申请时间" width="180">
          <template #default="{ row }">
            {{ formatDateTime(row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="审核时间" width="180">
          <template #default="{ row }">
            {{ row.auditTime ? formatDateTime(row.auditTime) : '-' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="100">
          <template #default="{ row }">
            <el-button type="primary" link @click="viewDetail(row)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination" v-if="total > 0">
        <el-pagination
          v-model:current-page="current"
          v-model:page-size="size"
          :total="total"
          layout="total, prev, pager, next"
          @current-change="loadData"
        />
      </div>
    </el-card>

    <!-- 详情对话框 -->
    <el-dialog v-model="dialogVisible" title="认领详情" width="600px">
      <el-descriptions :column="1" border v-if="currentClaim">
        <el-descriptions-item label="申请编号">{{ currentClaim.requestNo }}</el-descriptions-item>
        <el-descriptions-item label="申请人姓名">{{ currentClaim.applicantName }}</el-descriptions-item>
        <el-descriptions-item label="学号/工号">{{ currentClaim.applicantStudentNo }}</el-descriptions-item>
        <el-descriptions-item label="认领理由">{{ currentClaim.claimReason }}</el-descriptions-item>
        <el-descriptions-item label="申请状态">
          <el-tag :type="getStatusType(currentClaim.status)">
            {{ getStatusName(currentClaim.status) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="审核意见" v-if="currentClaim.auditComment">
          {{ currentClaim.auditComment }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getMyClaims } from '@/api/claim'

const loading = ref(false)
const tableData = ref([])
const total = ref(0)
const current = ref(1)
const size = ref(10)
const dialogVisible = ref(false)
const currentClaim = ref(null)

const statusNames = ['待审核', '审核通过', '审核驳回', '已完成']

const getStatusName = (status) => statusNames[status] || '未知'

const getStatusType = (status) => {
  const types = ['info', 'success', 'danger', 'success']
  return types[status] || 'info'
}

const formatDateTime = (dateTime) => {
  return new Date(dateTime).toLocaleString()
}

const viewDetail = (claim) => {
  currentClaim.value = claim
  dialogVisible.value = true
}

const loadData = async () => {
  loading.value = true
  try {
    const res = await getMyClaims({ current: current.value, size: size.value })
    tableData.value = res.data.records
    total.value = res.data.total
  } catch (error) {
    console.error(error)
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.my-claims-container {
  max-width: 1200px;
  margin: 0 auto;
}

.card-header {
  font-size: 18px;
  font-weight: bold;
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>

